<?php
function addRssFeed($cat_id, $title, $url)
{
	$title = strip_tags(trim($title));
	$url = trim($url);
	if($title != '')
	{
		if(($url != '') && (getParseFeed($url, 1) != '')) 
		{
			$insert  = 'INSERT INTO '.DB_PERFIX.'rss_feeds (`id`, `cat_id`, `name`, `url`)';
			$insert .= 'VALUES (NULL, '.$cat_id.', "'.$title.'", "'.$url.'");';
			$result = mysql_query($insert);
			
			if($result > 0)
			{ $error['feed_title'] = 'New rss feed added'; }
		}
		else
		{ $error['feed_url'] = 'Invalid feed URL'; }
	}
	else
	{ $error['feed_title'] = 'Invalid title'; }
	return $error;
}

function deleteRssFeed($id)
{
	if($id > 0)
	{
		$delete = 'DELETE FROM '.DB_PERFIX.'rss_feeds WHERE id='.$id.';';
		$result = mysql_query($delete);
		if($result > 0)
		{ return 'RSS Feed Deleted';}
		else
		{ return; }
	}
}

function getRssFeed($id = 0)
{
	if($id > 0)
	{
		$select = 'SELECT * FROM '.DB_PERFIX.'rss_feeds WHERE id='.$id.';';
		$result = mysql_query($select);
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['name'] = $row['name'];
				$item['url'] = $row['url'];
				$item['last_update'] = $row['last_update'];
			}
			return $item;
		}
	}
}
function getRssFeeds($cat_id)
{
	$select = 'SELECT id, name, url, last_update, feed_order, custom, store, active FROM '.DB_PERFIX.'rss_feeds WHERE cat_id='.$cat_id.';';
	$result = mysql_query($select);
	if(mysql_num_rows($result) > 0)
	{
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$item[$i]['url'] = $row['url'];
			$item[$i]['last_update'] = $row['last_update'];
			$i++;
		}
		return $item;
	}
}

function getParseFeed($url, $max=50, $id=1)
{
	$xmlStr = file_get_contents($url);
	if($xmlStr)
	{
		file_put_contents(CACHE_PATH.'feed_'.$id.'.xml', $xmlStr);
		$xmlDoc = new DOMDocument();
		
		if($xmlDoc->loadXML($xmlStr, LIBXML_NOERROR))
		{
			$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
		
			$rssfeed['ch_title'] = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
			$rssfeed['ch_link'] = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
			$rssfeed['ch_description'] = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
			
			$x=$xmlDoc->getElementsByTagName('item');
			for ($i=0; $i < $max; $i++)
			{
				if($x->item($i))
				{
					$rssfeed['item'][$i]['title'] 			= $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
					$rssfeed['item'][$i]['link'] 			= $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
					if($x->item($i)->getElementsByTagName('description')->item(0))
					{ $rssfeed['item'][$i]['description'] 	= $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue; }
					
					if($rssfeed['item'][$i]['title'] != '')
					{ $rssfeed['item'][$i]['title'] = trim($rssfeed['item'][$i]['title']); }
				}
			}
			return $rssfeed;
		}
		else
		{ return; }
	}
}
?>